﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BatailleNavale.Properties;
using System.Collections;

namespace BatailleNavale
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Placement pl = new Placement();
        ZoneAdver za = new ZoneAdver();
        Robot rob = new Robot();

        public void AddBateauxRobot(List<Bateau> lesBateaux)
        {
            rob.AjoutDesBateaux(lesBateaux);
        }
        protected void ClickDeCase(object sender, System.EventArgs e)
        {
            PictureBox pB = (PictureBox)sender;

            if (pl.GetBateauSelect())
            {
                pl.ClickPlacementBateau(pB.Name, pB);
            }
            //MessageBox.Show(pB.Name);
        }
        protected void ClickBateauAdversaire(object sender, System.EventArgs e)
        {
            bool touche =false;
            PictureBox pB = (PictureBox)sender;
            foreach (Bateau b in za.getBateaux())
            {
                foreach (string cases in b.getCases())
                {
                    if (cases == pB.Name)
                    {
                        pB.Image = BatailleNavale.Properties.Resources.explosion;
                        touche = true;
                        if (b.AddDegatBateau())
                        {
                            MessageBox.Show("Touché, coulé");
                            switch (b.getTaille())
                            {
                                case 5:
                                    porteAvionGif.Visible = true;
                                    porteAvionNB.Visible = true;
                                    break;
                                case 4:
                                    if (croiseurGif.Visible)
                                    {
                                        croiseurNB.Text = "x2";
                                    }
                                    else
                                    {
                                        croiseurGif.Visible = true;
                                        croiseurNB.Visible = true;
                                    }
                                    break;
                                case 3:
                                    if (sousMarinGif.Visible)
                                    {
                                        sousMarinNB.Text = "x2";
                                    }
                                    else 
                                    {
                                        sousMarinGif.Visible = true;
                                        sousMarinNB.Visible = true;
                                    }
                                    break;
                                case 2:
                                    if (torpilleurGif.Visible)
                                    {
                                        torpilleurNB.Text = "x2";
                                    }
                                    else
                                    {
                                        torpilleurGif.Visible = true;
                                        torpilleurNB.Visible = true;
                                    }
                                    break;
                                default:
                                    break;    
                            }
                            if (porteAvionGif.Visible && torpilleurNB.Text == "x2" && sousMarinNB.Text == "x2" && croiseurNB.Text == "x2")
                            {
                                MessageBox.Show("Bravo, vous avez gagné");
                            }
                        }
                    }
                }
            }
            if (touche == false)
            {
                pB.Image = BatailleNavale.Properties.Resources.cercleVert;
            }
            pB.Click -= new System.EventHandler(this.ClickBateauAdversaire);
            rob.TirerZoneJoueur();
        }

        public void BateauAdversaire()
        {
            foreach (PictureBox p in panel2Tab.Controls)
            {
                p.Click += new System.EventHandler(this.ClickBateauAdversaire);
            }
 
        }

        public void InitCases()
        {
            foreach (PictureBox p in panel1Tab.Controls)
            {
                p.Name = pl.GetCodeCase(p.Location.X, p.Location.Y);
                p.SizeMode = PictureBoxSizeMode.StretchImage;
                p.Image = BatailleNavale.Properties.Resources.eau;
                p.BorderStyle = BorderStyle.FixedSingle;
                p.Click += new System.EventHandler(this.ClickDeCase);
            }
            foreach (PictureBox p in panel2Tab.Controls)
            {
                p.SizeMode = PictureBoxSizeMode.StretchImage;
                p.Name = pl.GetCodeCase(p.Location.X, p.Location.Y);
                p.Image = BatailleNavale.Properties.Resources.eau;
                p.BorderStyle = BorderStyle.FixedSingle;
            }
            panel1Tab.Visible = true;
            panel2Tab.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            InitCases();
            pl.addForm(this);
            rob.AddForm(this);

        }

        private void remettreEau()
        {
            foreach (PictureBox pB in panel1Tab.Controls)
            {
                if (pB.ImageLocation == "noir.gif")
                {
                    pB.Image = BatailleNavale.Properties.Resources.eau;
                }
            }

        }

        #region Click des bordures de bateaux
        private void porteAvionGif_Click(object sender, EventArgs e)
        {
            pl.BateauClicked(false, 0);
            croiseurGif.BorderStyle = BorderStyle.None;
            sousMarinGif.BorderStyle = BorderStyle.None;
            torpilleurGif.BorderStyle = BorderStyle.None;
            remettreEau();

            if (porteAvionGif.BorderStyle == BorderStyle.None)
            {
                porteAvionGif.BorderStyle = BorderStyle.FixedSingle;
                pl.BateauClicked(true,5);
            }

            else {
                
                porteAvionGif.BorderStyle = BorderStyle.None;

            }

        }


        private void croiseurGif_Click(object sender, EventArgs e)
        {
            pl.BateauClicked(false, 0);
            porteAvionGif.BorderStyle = BorderStyle.None;
            sousMarinGif.BorderStyle = BorderStyle.None;
            torpilleurGif.BorderStyle = BorderStyle.None;
            remettreEau();

            if (croiseurGif.BorderStyle == BorderStyle.None)
            {
                croiseurGif.BorderStyle = BorderStyle.FixedSingle;
                pl.BateauClicked(true,4);
            }
            else
            {
                croiseurGif.BorderStyle = BorderStyle.None;
                
            }
        }

        private void sousMarinGif_Click(object sender, EventArgs e)
        {
            pl.BateauClicked(false, 0);
            porteAvionGif.BorderStyle = BorderStyle.None;
            croiseurGif.BorderStyle = BorderStyle.None;
            torpilleurGif.BorderStyle = BorderStyle.None;
            remettreEau();

            if (sousMarinGif.BorderStyle == BorderStyle.None)
            {
                sousMarinGif.BorderStyle = BorderStyle.FixedSingle;
                pl.BateauClicked(true,3);
            }

            else{
                sousMarinGif.BorderStyle = BorderStyle.None;
            }

        }

        private void torpilleurGif_Click(object sender, EventArgs e)
        {
            pl.BateauClicked(false,0);
            porteAvionGif.BorderStyle = BorderStyle.None;
            croiseurGif.BorderStyle = BorderStyle.None;
            sousMarinGif.BorderStyle = BorderStyle.None;
            remettreEau();

            if (torpilleurGif.BorderStyle == BorderStyle.None)
            {
                torpilleurGif.BorderStyle = BorderStyle.FixedSingle;
                pl.BateauClicked(true,2);
            }

            else
            {
                torpilleurGif.BorderStyle = BorderStyle.None;
            }
        }
        #endregion

        public void DessinBat(string[] casesBateau, string typeBateau, char verthor)
        {
            int i = 1;
            foreach (string caseB in casesBateau)
            {
                string nomImage = typeBateau + "-" + verthor.ToString() + i.ToString() + ".gif";
                foreach (PictureBox pB in panel1Tab.Controls)
                {
                    if (pB.Name == caseB)
                    {
                        pB.ImageLocation = nomImage;
                    }
                }
                i++;
            }
        }

        private void btMenu_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btRecommencer_Click(object sender, EventArgs e)
        {
            DialogResult dr = new DialogResult();
            Form1 f1 = new Form1();
            this.Hide();
            dr = f1.ShowDialog();
        }
    }
}
